﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BGCOP.CAVTOP.BUSINESSLAYER;

namespace BGCOP.CAVTOP.WINFORMS
{
    public partial class frmVehiculo : Form
    {
        public Font c39Font;
        private PrivateFontCollection pfc;
        private FontFamily family;
        private Graphics objGraphics;
        public frmVehiculo()
        {
            InitializeComponent();
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public void inicioFuneteCB()
        {
            pfc = new PrivateFontCollection();
            string path;
            path = Path.GetDirectoryName(Application.ExecutablePath);
            // Load in the temporary barcode font

            //pfc.AddFontFile(path + "\\DLLWEB\\code128.ttf");
            //family = new FontFamily("Code 128", pfc);

            pfc.AddFontFile(path + "\\DLLWEB\\free3of9.ttf");
            family = new FontFamily("Free 3 of 9", pfc);


            // Select the font family to use
            

            // Create the font object with size 30
            c39Font = new Font(family, 40);
            //label8.Font = c39Font;
            //label8.Text = "*12345*";
        }
        public void dCB(string CB)
        {
            
            objGraphics = pictureBox1.CreateGraphics();
            objGraphics.Clear(Color.White);
            objGraphics.DrawString("*" + CB + "*", c39Font, new SolidBrush(Color.Black), 0, 0);
            
        }
        private void frmVehiculo_Load(object sender, EventArgs e)
        {

            timer1.Enabled = true;

        }
        public void llenaLista()
        {
            Vehiculos tmp = new Vehiculos();
            listBox1.DataSource = tmp.ListarTodos();
            listBox1.DisplayMember = "Placa";
            listBox1.ValueMember = "idVehiculo";
            
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
            {
                return;
            }
            
            TVehiculo  tmp = (TVehiculo)listBox1.SelectedItem;
            txtPlaca.Text = tmp.Placa;
            txtColor.Text = tmp.color;
            txtCodB.Text = tmp.CodigodeBarras;
            dCB(tmp.CodigodeBarras);
            //buscar modelo
            int i = 0;
            TModeloVehiculo ty= new TModeloVehiculo();
            foreach (TModeloVehiculo ty1 in comboBox1.Items)
            {
                if (ty1.idModeloVehiculo == tmp.idModeloVehiculo)
                {
                    ty = ty1;
                    break;
                }
                i++;
            }
            try
            {
                comboBox1.SelectedIndex = i;
                //ty.idMarcaVehiculo 
                i = 0;
                i = comboBox2.Items.Cast<TMarcaVehiculo>().TakeWhile(qqq => qqq.idMarcaVehiculo != ty.idMarcaVehiculo).Count();
                try
                {
                    comboBox2.SelectedIndex = i;
                }
                catch (Exception)
                {
                    
                    //throw;
                }
            }
            catch (Exception)
            {
                
                //throw;
            }
            
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                Vehiculos tmp = new Vehiculos();
                if (MessageBox.Show("Desea eliminar el registro seleccionado?", Properties.Settings.Default.Titulo,
                MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
                {
                    tmp.Eliminar(Convert.ToInt32(listBox1.SelectedValue));
                    MessageBox.Show("El registro se elimino con exito!", Properties.Settings.Default.Titulo
                        , MessageBoxButtons.OK);
                    llenaLista();
                }

            }
        }

        private void btnModificar_Click(object sender, EventArgs e)
        {
            TVehiculo tmp = new TVehiculo();
            tmp.Placa = txtPlaca.Text;
            tmp.color = txtColor.Text;
            tmp.CodigodeBarras = txtCodB.Text;
            tmp.idModeloVehiculo = (short) comboBox1.SelectedValue;
            tmp.fechaUltimaGestion = DateTime.Now;
            tmp.usuarioUltimaGestion = (int?)BGCOP.CAVTOP.WINFORMS.Program.idUsuarioActual;
            Vehiculos mm = new Vehiculos();
            if (listBox1.SelectedIndex == -1)
            {
                //es un nuevo registro

                
                mm.Agregar(txtPlaca.Text, (short)comboBox1.SelectedValue,txtCodB.Text,txtColor.Text, DateTime.Now,
                    (int)BGCOP.CAVTOP.WINFORMS.Program.idUsuarioActual);
                MessageBox.Show("El registro se agrego con exito!", Properties.Settings.Default.Titulo
                    , MessageBoxButtons.OK);
                llenaLista();
            }
            else
            {
                //es un registro existente

                
                mm.Modificar(Convert.ToInt32(listBox1.SelectedValue), txtPlaca.Text, 
                    (short)comboBox1.SelectedValue, txtCodB.Text, txtColor.Text, DateTime.Now,
                    (int)BGCOP.CAVTOP.WINFORMS.Program.idUsuarioActual);
                MessageBox.Show("El registro se modificó con exito!", Properties.Settings.Default.Titulo
                   , MessageBoxButtons.OK);
                llenaLista();

            }
        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
            txtPlaca.Focus();
            listBox1.SelectedIndex = -1;
            comboBox2.SelectedIndex = -1;
            comboBox1.SelectedIndex = -1;
            txtColor.Text = "";
            txtCodB.Text = "";
            txtPlaca.Text = "";
        }

        private void frmVehiculo_Paint(object sender, PaintEventArgs e)
        {
           
        }

       private void button1_Click(object sender, EventArgs e)
        {
           printDocument1.OriginAtMargins = true;
           printDocument1.Print();
           //printPreviewDialog1.ShowDialog();


        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
            {
                return;
            }

            TVehiculo tmp = (TVehiculo)listBox1.SelectedItem;

            Graphics g;
            Bitmap bm = new Bitmap(500, 200);
            g = Graphics.FromImage(bm);  // buffer graphics
            g.Clear(Color.White);
            g.DrawString("*" + tmp.CodigodeBarras + "*", c39Font, new SolidBrush(Color.Black), 0, 0);
            e.Graphics.DrawImage(bm, 0, 0);

            //e.Graphics.DrawString("*" + tmp.CodigodeBarras + "*", new Font(family, 40), new SolidBrush(Color.Black), 0, 0);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Vehiculos vi = new Vehiculos();
            txtCodB.Text = vi.GenCodigodeBarras();
            dCB(txtCodB.Text);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            inicioFuneteCB();
            llenaLista();

            MarcasVehiculo e1 = new MarcasVehiculo();
            comboBox2.DataSource = e1.ListarTodos();
            comboBox2.DisplayMember = "NombreMarcaVehiculo";
            comboBox2.ValueMember = "idMarcaVehiculo";

            ModelosVehiculo e2 = new ModelosVehiculo();
            comboBox1.DataSource = e2.ListarTodos();
            comboBox1.DisplayMember = "NombreModelo";
            comboBox1.ValueMember = "idModeloVehiculo";
        }
    }
}
